<template>
  <div class="AddOrUpdate_main add_container column_three">
    <div class="main_main">
      <el-form ref="form" :model="formData" :rules="rules" :inline="true">
        <el-form-item label="生产检验单号" prop="code">
          <CodeRuleInput :band-value.sync="formData.code" table-name="product_inspection_check" column-name="CODE" :is-edit="id" />
        </el-form-item>
        <el-form-item label="检验人员" prop="checkByName">
          <el-input v-model="formData.checkByName" placeholder="暂无" disabled />
        </el-form-item>
        <el-form-item label="检验时间" prop="checkDate">
          <el-date-picker
            v-model="formData.checkDate"
            type="datetime"
            value-format="yyyy-MM-dd HH"
            format="yyyy-MM-dd HH"
            placeholder="请选择"
            :editable="false"
            disabled
          />
        </el-form-item>
        <el-form-item v-if="!show" label="物料编码" class="clickChoose" prop="materialCode">
          <el-input v-model="formData.materialCode" placeholder="暂无" readonly />
          <a
            class="clickChoose_button"
            @click.prevent="chooseMaterialInfo"
          >
            <svg-icon icon-class="选择弹框" />
          </a>
        </el-form-item>
        <el-form-item v-else label="物料编码" class="clickChoose" prop="materialCode">
          <el-input v-model="formData.materialCode" disabled />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="formData.materialName" placeholder="暂无" disabled />
        </el-form-item>
        <el-form-item label="规格型号" prop="materialModel">
          <el-input v-model="formData.materialModel" placeholder="暂无" disabled />
        </el-form-item>
        <el-form-item label="物料单位" prop="materialUnitName">
          <el-input v-model="formData.materialUnitName" placeholder="暂无" disabled />
        </el-form-item>
        <el-form-item label="关联任务单" prop="productTaskCode">
          <el-input v-model="formData.productTaskCode" disabled />
        </el-form-item>
        <el-form-item v-if="!this.show" label="工艺要求" class="clickChoose" prop="engineeringRequirementCode">
          <el-input v-model="formData.engineeringRequirementCode" placeholder="暂无" readonly />
          <a
            class="clickChoose_button"
            @click.prevent="getProcessRequest"
          >
            <svg-icon icon-class="选择弹框" />
          </a>
        </el-form-item>
        <el-form-item v-else label="工艺要求" class="clickChoose" prop="engineeringRequirementCode">
          <el-input v-model="formData.engineeringRequirementCode" placeholder="暂无" disabled readonly />
        </el-form-item>
        <el-form-item v-if="!isShow" label="工序" class="clickChoose" prop="engineeringProcessName">
          <el-input v-model="formData.engineeringProcessName" placeholder="暂无" readonly />
          <a
            class="clickChoose_button"
            @click.prevent="getProcess"
          >
            <svg-icon icon-class="选择弹框" />
          </a>
        </el-form-item>
        <el-form-item v-else label="工序" class="clickChoose" prop="engineeringProcessName">
          <el-input v-model="formData.engineeringProcessName" placeholder="暂无" disabled readonly />
        </el-form-item>
        <el-form-item label="检验模板" class="clickChoose" prop="qualityTemplateName">
          <el-input v-model="formData.qualityTemplateName" placeholder="暂无" readonly />
          <a
            class="clickChoose_button"
            @click.prevent="choosePeople"
          >
            <svg-icon icon-class="选择弹框" />
          </a>
        </el-form-item>
        <el-form-item v-if="!show" label="送检数量" prop="checkNumber">
          <el-input v-model="formData.checkNumber" placeholder="请输入" @input="changInspectNumber($event)" @blur="checkNumberHandleBlur()" />
        </el-form-item>
        <el-form-item v-else label="送检数量" prop="checkNumber">
          <el-input v-model="formData.checkNumber" disabled />
        </el-form-item>
        <el-form-item label="抽检数量" prop="spotCheckNumber">
          <el-input v-model="formData.spotCheckNumber" placeholder="请输入" @blur="spotCheckNumberHandleBlur($event)" />
        </el-form-item>
        <el-form-item label="不合格数量">
          <el-input v-model="formData.disqualificationNumber" placeholder="暂无" disabled @input="changeTaxPrice" />
        </el-form-item>
        <el-form-item label="合格数量" prop="qualifiedNumber" class="qualifiedNumber">
          <el-input v-model="formData.qualifiedNumber" placeholder="暂无" disabled />
        </el-form-item>
        <el-form-item label="判定结果" prop="resultCode">
          <el-radio-group v-model="formData.resultCode">
            <el-radio label="通过">通过</el-radio>
            <el-radio label="不通过">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="formData.resultCode == '通过'" label="是否入库" prop="isInForm">
          <el-radio-group v-model="formData.isInForm">
            <el-radio :disabled="formData.automaticCreate == '1' && formData.isInForm == '不入库'" label="入库">入库</el-radio>
            <el-radio :disabled="formData.resultCode == '通过' && formData.automaticCreate == '1' && formData.isInForm == '入库'" label="不入库">不入库</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="附件" class="double">
          <FileManageFull
            :file-names="formData.fileName"
            :file-real-names="formData.fileRealName"
            :file-urls="formData.url"
            :row-index="1"
            @fileName="setFileName"
          />
        </el-form-item>
        <el-form-item label="不良描述" class="double">
          <el-input
            v-model="formData.description"
            type="textarea"
            placeholder="请输入"
            maxlength="200"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="检验图例" class="double">
          <FileManageFull
            :file-names="formData.templateFileNames"
            :file-real-names="formData.templateFileRealNames"
            :file-urls="formData.templateFileUrls"
            :row-index="1"
            :readonly="true"
            @fileName="setFileName2"
          />
        </el-form-item>
      </el-form>
      <div class="formItem_header1">
        整体项目
      </div>
      <common1 ref="common1" :parent="this" @my-event="parentMethod" />
      <div class="formItem_header1">
        个体项目
      </div>
      <common ref="common" :parent="this" @my-event="parentMethod" />
    </div>
    <div class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button v-if="formData.productTaskCode" @click.stop="save">保存</el-button>
      <el-button v-else @click.stop="save2">存草稿</el-button>
      <el-button @click.stop="onSubmit">提交</el-button>
    </div>
  </div>
</template>
<script>
import { addOrUpdate, getInfo } from '@/api/qualityManage/productionInspectionForm'
import addCheckoutTemplate from '../checkoutTemplate/addCheckoutTemplate'
import addProcess from './addProcess'
import addCraftRequest from './addCraftRequest'
import moment from 'moment'
import addMaterialInfo from '@/views/warehouseManagement/materialInfo/addMaterialInfoOne'
import Big from 'big.js'
import common from '../common/index'
import common1 from '../common/index2'
export default {
  name: 'AddOrUpdate',
  components: {
    common,
    common1
  },
  props: {
    layerid: {},
    id: {},
    defaultValue: {},
    show: {}
  },
  data() {
    const checkNumber = (rule, value, callback) => {
      if (!this.formData.checkNumber) {
        callback(new Error('送检数量不能为空'))
      } else if (Number(this.formData.checkNumber) < Number(this.formData.disqualificationNumber)) {
        callback(new Error('送检数量不能小于不合格数量'))
      } else if (Number(this.formData.checkNumber) < Number(this.formData.spotCheckNumber)) {
        callback(new Error('送检数量不能小于抽检数量'))
      } else if (this.formData.checkNumber && !this.SETTINGS.reg_no_zero.test(this.formData.checkNumber)) {
        callback(new Error('送检数量填写不正确'))
      } else {
        callback()
      }
    }
    const disqualificationNumber = (rule, value, callback) => {
      if (!this.formData.disqualificationNumber) {
        callback(new Error('不合格数量不能为空'))
      } else if (Number(this.formData.disqualificationNumber) > Number(this.formData.checkNumber)) {
        callback(new Error('不合格数量不能大于送检数量'))
      } else if (this.formData.checkNumber && !this.SETTINGS.reg_zero.test(this.formData.disqualificationNumber)) {
        callback(new Error('不合格数量填写不正确'))
      } else {
        callback()
      }
    }
    const spotCheckNumber = (rule, value, callback) => {
      if (value && Number(value) > Number(this.formData.checkNumber)) {
        callback(new Error('抽检数量不能大于送检数量'))
      } else if (value && value <= 0) {
        callback(new Error('送检数量不能小于0'))
      } else {
        callback()
      }
    }
    return {
      pageId: '生产检验单', // 页面唯一标识
      formData: {
        id: undefined,
        code: undefined,
        name: undefined,
        roleIdsArray: undefined,
        roleIds: undefined,
        model: undefined,
        remark: undefined,
        resultCode: '通过',
        fileName: '',
        fileRealName: '',
        url: '',
        isInForm: '入库',
        status: 1,
        data: [],
        templateFileNames: '',
        templateFileRealNames: '',
        templateFileUrls: '',
        engineeringRequirementCode: '',
        engineeringRequirementId: '',
        engineeringRequirementMaterialId: '',
        isMakeReport: '0',
        materialCode: undefined,
        materialName: undefined,
        materialModel: undefined,
        materialUnitName: undefined,
        productTaskCode: undefined,
        materialId: undefined,
        qualityTemplateName: undefined,
        spotCheckNumber: undefined,
        engineeringProcessId: undefined,
        engineeringProcessName: undefined
      },
      foDate: {
        companyId: undefined,
        status: 1
      },
      orderId: '',
      isShow: false,
      userOptions: [
        { id: '是', label: '是' },
        { id: '否', label: '否' }
      ],
      equipmentList: [],
      selectionsAll1: [],
      ids: [],
      roleOptions: [],
      selectionsAll: [], // 表格所有页选中的行
      inx: 1,
      MaterialList: [],
      listData1: [],
      tableData2: [],
      tableHeight: '400',
      tableData: [], // 表格数据
      tableData1: [], // 表格拷贝数据
      listData: [], // 表头数据
      currentPage: 1, // 当前页数
      pageSize: 10, // 每页显示的数据条数
      rules: {
        qualityTemplateName: [
          { required: true, message: '检验模板不能为空', trigger: 'change' }
        ],
        code: [
          { required: true, message: '生产检验单号不能为空', trigger: 'blur' }
        ],
        checkNumber: [
          { validator: checkNumber, required: true, trigger: 'blur' }
        ],
        disqualificationNumber: [
          { validator: disqualificationNumber, required: true, trigger: 'blur' }
        ],
        spotCheckNumber: [
          { validator: spotCheckNumber, trigger: 'blur' }
        ],
        resultCode: [
          { required: true, message: '判定结果不能为空', trigger: 'change' }
        ],
        materialCode: [
          { required: true, message: '物料编码不能为空', trigger: 'change' }
        ],
        isInForm: [
          { required: true, message: '是否入库不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    if (!this.id) { // 没有id就是新增
    } else { // 有id就是编辑
      this.getInfo()
      this.isShow = this.show
    }
  },
  mounted() {
    this.formData.checkByName = this.$store.state.user.name
    this.formData.checkBy = this.$store.state.user.userId
    this.formData.checkDate = moment(new Date()).format('YYYY-MM-DD HH')
  },
  methods: {
    parentMethod(e) {
      this.$set(this.formData, 'disqualificationNumber', e)
      this.changeTaxPrice()
    },
    checkNumberHandleBlur() { // 送检数量失焦
      if (!this.formData.spotCheckNumber || this.formData.spotCheckNumber == '0') {
        this.tableData1 = JSON.parse(JSON.stringify(this.tableData))
        this.tableData = []
        this.currentPage = 1
        this.handleBlur(this.formData.checkNumber)
        this.$refs.common.getSingleDecide()
      } else {
        this.spotCheckNumberHandleBlur()
      }
    },
    spotCheckNumberHandleBlur() { // 抽检数量失焦
      if (Number(this.formData.spotCheckNumber) <= Number(this.formData.checkNumber)) {
        if (!this.formData.spotCheckNumber || this.formData.spotCheckNumber == '0') {
          this.handleBlur(this.formData.checkNumber)
        } else {
          this.handleBlur(this.formData.spotCheckNumber)
        }
        this.$refs.common.getSingleDecide()
        // 之前有数据需要把数据存起来 比对
        this.tableData1 = JSON.parse(JSON.stringify(this.tableData))
        this.tableData = []
        this.currentPage = 1
        if (!this.formData.spotCheckNumber || this.formData.spotCheckNumber == '0') {
          this.handleBlur(this.formData.checkNumber)
        } else {
          this.handleBlur(this.formData.spotCheckNumber)
        }
        this.$refs.common.getSingleDecide()
      }
    },
    handleBlur(number) { // 失焦事件处理逻辑
      // 根据输入框生成数据
      if (Number(number) >= 10000) {
        number = 10000
      }
      const newData = Array.from({ length: parseInt(number) }, (_, i) => ({ id: 1 + i, qualityItemName: '', singleDecide: '', materialName: `${this.formData.materialName ? this.formData.materialName + '-' : ''}${i + 1} ` }))
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      const arrData = number >= end ? newData.slice(start, end) : newData.slice(start)
      const result = arrData.map(item => {
        const index = this.tableData1.findIndex(obj => obj.id == item.id)
        if (index !== -1) {
          return { ...item, ...this.tableData1[index] }
        }
        return item
      })
      this.tableData = this.tableData.concat(result)
      this.tableData = [...new Map(this.tableData.map(item => [item.id, item])).values()]
      this.scrollTop = 0
      if (this.listData.length <= 0) {
        this.tableData = []
      }
    },
    handleBlur1(number) { // 失焦事件处理逻辑
      // 根据输入框生成数据
      if (Number(number) >= 10000) {
        number = 10000
      }
      const newData = Array.from({ length: parseInt(number) }, (_, i) => ({ id: 1 + i, qualityItemName: '', singleDecide: '', materialName: `${this.formData.materialName ? this.formData.materialName : ''} ` }))
      // const start = (this.currentPage - 1) * this.pageSize
      // const end = start + this.pageSize
      // const arrData = number >= end ? newData.slice(start, end) : newData.slice(start)
      const result = newData.map(item => {
        const index = this.tableData1.findIndex(obj => obj.id == item.id)
        if (index !== -1) {
          return { ...item, ...this.tableData1[index] }
        }
        return item
      })
      // this.tableData = this.tableData.concat(result)
      this.tableData2 = result
      // this.tableData = [...new Map(this.tableData.map(item => [item.id, item])).values()]
      this.tableData2 = [...new Map(this.tableData2.map(item => [item.id, item])).values()]
      this.scrollTop = 0
      if (this.listData1.length <= 0) {
        this.tableData2 = []
      }
    },
    setFileName(...data) {
      this.$set(this.formData, 'fileName', data[0])
      this.$set(this.formData, 'fileRealName', data[1])
      this.$set(this.formData, 'url', data[2])
    },
    // headerCellClassName(data) {
    //   if (['singleDecide'].indexOf(data.column.property) != -1) {
    //     return 'column_bt'
    //   }
    // },
    // selectionChange1(rows) {
    //   this.selectionsAll1 = rows
    // },
    chooseCustomer(row) {
      this.formData.engineeringProcessId = row.engineeringProcessId
      this.formData.engineeringProcessName = row.name
      this.formData.engineeringRequirementMaterialId = row.id
    },
    getInfo() {
      getInfo(this.id).then(res => {
        this.orderId = res.data.purchaseOrderId
        this.formData = res.data
        this.$nextTick(() => {
          this.$refs.form.clearValidate('resultCode')
        })
        this.formData.qualifiedNumber = new Big(this.formData.checkNumber).minus(this.formData.disqualificationNumber).toString()
        if (res.data.isInForm) {
          this.formData.isInForm = res.data.isInForm
        } else {
          this.formData.isInForm = '入库'
        }
        if (!this.formData.qualityTemplateName) {
          this.formData.qualityTemplateName = undefined
        } else if (!this.formData.disqualificationNumber) {
          this.formData.disqualificationNumber = undefined
        }
        this.formData.resultCode += ''
        if (!this.formData.checkDate) {
          this.formData.checkDate = moment(new Date()).format('YYYY-MM-DD HH')
        }
        if (!this.formData.checkBy) {
          this.formData.checkByName = this.$store.state.user.name
          this.formData.checkBy = this.$store.state.user.userId
        }
        // 获取表格数据
        // if (res.data.titleJson && res.data.qualityJson) {
        //   this.listData = JSON.parse(res.data.titleJson)
        //   const start = (this.currentPage - 1) * this.pageSize
        //   const end = start + this.pageSize
        //   const newData1 = JSON.parse(res.data.qualityJson)
        //   this.tableData1 = JSON.parse(JSON.stringify(newData1))
        //   this.tableData = this.tableData.concat(res.data.spotCheckNumber >= end ? newData1.slice(start, end) : newData1.slice(start))
        // } else {
        //   if (res.data.spotCheckNumber && res.data.spotCheckNumber !== '0') {
        //     this.handleBlur(res.data.spotCheckNumber)
        //   } else {
        //     this.handleBlur(res.data.checkNumber)
        //   }
        // }
        if (res.data.titleJson && res.data.qualityJson) {
          this.listData = JSON.parse(res.data.titleJson)
          this.listData1 = JSON.parse(res.data.entityTitleJson)
          this.tableData2 = JSON.parse(res.data.entityQualityJson)
          const newData1 = JSON.parse(res.data.qualityJson)
          this.tableData1 = JSON.parse(JSON.stringify(newData1))
          if (res.data.spotCheckNumber && res.data.spotCheckNumber !== '0') {
            this.handleBlur(res.data.spotCheckNumber)
          } else {
            this.handleBlur(res.data.checkNumber)
          }
        } else {
          if (res.data.spotCheckNumber && res.data.spotCheckNumber !== '0') {
            this.handleBlur(res.data.spotCheckNumber)
          } else {
            this.handleBlur(res.data.checkNumber)
          }
        }
      })
      // 原数据处理逻辑
      this.tableData.forEach(item => {
        item.singleDecide = Number(item.singleDecide)
        if (!item.singleDecide) {
          item.singleDecide = ''
        }
      })
    },
    // getOrderGetInfo() {
    //   orderGetInfo(this.orderId).then(res => {
    //     this.listDate = res.data.detailList
    //     this.listDate.forEach(item => {
    //       item.purchaseOrderDetailId = item.id
    //       item.id = ''
    //       item.remark = ''
    //     })
    //   })
    // },
    // remove1() {
    //   this.selectionsAll1.forEach(item => {
    //     this.listData.splice(this.listData.indexOf(item), 1)
    //   })
    // },
    // 保存
    save() {
      let tag = true
      this.$refs.form.clearValidate('disqualificationNumber')
      this.$refs.form.clearValidate('resultCode')
      this.$refs['form'].validateField(['code', 'qualityTemplateName', 'checkNumber', 'materialCode', 'spotCheckNumber'], (valid) => {
        if (tag && valid) {
          tag = false
        }
      })
      if (tag) {
        this.tableData.forEach(item => {
          if (!item.id) {
            delete item.id
          }
        })
        this.formData.status = 1
        const combinedArray = [...this.tableData, ...this.tableData2]
        // 遍历合并后的数组，检查 singleDecide_ 和 itemPropertyName 字段是否有值
        for (let i = 0; i < combinedArray.length; i++) {
          // 获取当前对象的所有属性名
          const keys = Object.keys(combinedArray[i])
          // 遍历属性名，检查是否有 singleDecide_ 或 itemPropertyName 开头的属性有值
          for (let j = 0; j < keys.length; j++) {
            if (keys[j].startsWith('singleDecide_') || keys[j].startsWith('itemPropertyName')) {
              if (combinedArray[i][keys[j]]) {
                this.formData.status = 2
                break // 如果找到一个有值的字段，就跳出循环
              }
            }
          }
          // 如果已经设置了 status，则跳出外层循环
          if (this.formData.status === 2) {
            break
          }
        }
        // 将数据处理成JSON格式
        this.formData.qualityJson = JSON.stringify(this.tableData)
        this.formData.entityQualityJson = JSON.stringify(this.tableData2)
        this.formData.titleJson = JSON.stringify(this.listData)
        this.formData.entityTitleJson = JSON.stringify(this.listData1)
        addOrUpdate(this.formData).then(res => {
          this.$message({
            message: res.msg,
            type: 'success'
          })
          this.$layer.close(this.layerid) // 关闭弹框
          this.$parent.refresh() // 调用父组件的刷新方法
        })
      }
    },
    save2() {
      let tag = true
      this.$refs.form.clearValidate('disqualificationNumber')
      this.$refs.form.clearValidate('resultCode')
      this.$refs['form'].validateField(['code', 'qualityTemplateName', 'checkNumber', 'materialCode'], (valid) => {
        if (tag && valid) {
          tag = false
        }
      })
      if (tag) {
        this.tableData.forEach(item => {
          if (!item.id) {
            delete item.id
          }
        })

        this.formData.status = 0
        // 将数据处理成JSON格式
        this.formData.qualityJson = JSON.stringify(this.tableData)
        this.formData.entityQualityJson = JSON.stringify(this.tableData2)
        this.formData.titleJson = JSON.stringify(this.listData)
        this.formData.entityTitleJson = JSON.stringify(this.listData1)
        addOrUpdate(this.formData).then(res => {
          this.$message({
            message: res.msg,
            type: 'success'
          })
          this.$layer.close(this.layerid) // 关闭弹框
          this.$parent.refresh() // 调用父组件的刷新方法
        })
      }
    },
    // 提交
    onSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.tableData.forEach(item => {
            if (!item.id) {
              delete item.id
            }
          })
          let isSingle = true
          let isSingle1 = true
          let isRecord = false
          this.listData1.forEach((item, index) => {
            if (item.itemPropertyName == '判定项') {
              isSingle = false
            } else {
              isRecord = true
            }
            if (item.qualityItemName == '质保书' && item.itemPropertyName == '记录项') {
              this.rowIndex = index
            }
          })
          if (!isSingle) {
            let isEmpty = false
            this.listData1.forEach((item, index) => {
              if (item.itemPropertyName == '判定项') {
                const fieldName = 'singleDecide_' + (index + 1)
                if (!this.tableData2[0][fieldName]) {
                  isEmpty = true
                  return
                }
              }
            })
            if (isEmpty) {
              this.$message.warning('整体项目的单项判定不能为空')
              return
            }
          }
          this.listData.forEach((item, index) => {
            if (item.itemPropertyName == '判定项') {
              isSingle1 = false
            } else {
              isRecord = true
            }
          })
          if (!isSingle1) {
            let isEmpty = false
            this.listData.forEach((item, index) => {
              if (item.itemPropertyName == '判定项') {
                const fieldName = 'singleDecide_' + (index + 1)
                this.tableData.forEach(ele => {
                  if (!ele[fieldName]) {
                    isEmpty = true
                    return
                  }
                })
              }
            })

            if (isEmpty) {
              this.$message.warning('个体项目的单项判定不能为空')
              return
            }
          }
          // 将数据处理成JSON格式
          this.formData.qualityJson = JSON.stringify(this.tableData)
          this.formData.entityQualityJson = JSON.stringify(this.tableData2)
          this.formData.titleJson = JSON.stringify(this.listData)
          this.formData.entityTitleJson = JSON.stringify(this.listData1)
          if (this.rowIndex) {
            this.tableData2.forEach(item => {
              if (!item[`itemPropertyName${this.rowIndex + 1}`]) {
                this.formData.isMakeReport = '1'
              } else {
                this.formData.isMakeReport = '0'
              }
            })
          }
          if (isRecord) {
            this.formData.canEdit = '1'
          }
          addOrUpdate({ ...this.formData, status: '3' }).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.$layer.close(this.layerid) // 关闭弹框
            this.$parent.refresh() // 调用父组件的刷新方法
          })
        }
      })
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    },
    chooseMaterialInfo() {
      const MaterialData = this.$deepClone(this.formData)
      this.MaterialList.push(MaterialData)
      this.$layer.iframe({
        // 弹框
        content: {
          content: addMaterialInfo, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            materialId: this.formData.materialId ? this.formData.materialId : ''
          } // props
        },
        area: ['1020px', '650px'],
        title: '选择物料', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    getProcess() {
      this.$layer.iframe({// 弹框
        content: {
          content: addProcess, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            selectedRow: this.tableData,
            id: this.formData.engineeringProcessId,
            engineeringRequirementId: this.formData.engineeringRequirementId
          }// props
        },
        area: ['1200px', '600px'],
        title: '选择工序', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    getProcessRequest() {
      if (this.formData.materialCode) {
        this.$layer.iframe({// 弹框
          content: {
            content: addCraftRequest, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              id: this.formData.materialId,
              engineeringRequirementId: this.formData.engineeringRequirementId
            }// props
          },
          area: ['1200px', '600px'],
          title: '选择工艺要求', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      } else {
        this.$message.warning('物料编码不能为空')
        return
      }
    },
    chooseCraftRequest(data) { // 工艺要求回调
      this.formData.engineeringRequirementCode = data.engineeringRequirementCode
      this.formData.engineeringRequirementMaterialId = data.id
      this.formData.engineeringRequirementId = data.engineeringRequirementId
      if (data.engineeringProcessName) {
        this.formData.engineeringProcessName = data.engineeringProcessName
        this.formData.engineeringProcessId = data.engineeringProcessId
        this.isShow = true
      }
    },
    choosePeople() {
      this.$layer.iframe({// 弹框
        content: {
          content: addCheckoutTemplate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            defaultValue: { id: this.formData.qualityTemplateId ? this.formData.qualityTemplateId : '',
              name: this.formData.qualityTemplateName ? this.formData.qualityTemplateName : '',
              fileUrl: this.formData.templateFileUrls ? this.formData.templateFileUrls : '',
              fileRealName: this.formData.templateFileRealNames ? this.formData.templateFileRealNames : '',
              fileName: this.formData.templateFileNames ? this.formData.templateFileNames : '' },
            selectedRow: [...this.listData, ...this.listData1]
          }// props
        },
        area: ['1200px', '600px'],
        title: '选择检验模版', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    // 不合格数量校验
    changeTaxPrice(e) {
      if (this.formData.checkNumber !== '' && this.formData.checkNumber !== undefined && this.formData.disqualificationNumber !== '' && this.formData.disqualificationNumber !== undefined) {
        this.formData.qualifiedNumber = new Big(this.formData.checkNumber).minus(this.formData.disqualificationNumber).toString()
      } else {
        this.formData.qualifiedNumber = undefined
      }
    },
    //  送检数量校验
    changInspectNumber() {
      if (this.formData.disqualificationNumber !== '' && this.formData.disqualificationNumber !== undefined && this.formData.checkNumber !== undefined && this.formData.checkNumber !== '') {
        this.formData.qualifiedNumber = new Big(this.formData.checkNumber).minus(this.formData.disqualificationNumber).toString()
      } else {
        this.formData.qualifiedNumber = undefined
      }
    },
    showTemplate(data, templateName, templateId, remark, templateItem) {
      this.listData = data.filter(item => item.itemDimensionName === '个体项目') // 个体项目
      this.listData1 = data.filter(item => item.itemDimensionName === '整体项目') // 整体项目
      this.tableData = []
      this.tableData2 = []
      if (this.listData1 && this.listData1.length > 0) {
        this.handleBlur1(1)
      }
      if (this.listData.length > 0) {
        this.checkNumberHandleBlur()
        this.spotCheckNumberHandleBlur()
        this.$refs.common.getSingleDecide()
      } else {
        this.formData.disqualificationNumber = 0
        this.formData.qualifiedNumber = 0
      }
      this.formData.templateFileNames = templateItem.fileName
      this.formData.templateFileRealNames = templateItem.fileRealName
      this.formData.templateFileUrls = templateItem.fileUrl
      this.formData.qualityTemplateName = templateName
      this.formData.qualityTemplateId = templateId
      this.listData.forEach(item => {
        item.qualityTemplateDetailId = item.id
        delete item.id
      })
      this.listData1.forEach(item => {
        item.qualityTemplateDetailId = item.id
        delete item.id
      })
    },
    setFileName2(...data) {
      this.formData.fileName = data[0]
      this.formData.fileRealName = data[1]
      this.formData.fileUrl = data[2]
    },
    showMaterial(row) {
      if (this.formData.materialCode !== row.materialCode && this.formData.materialId !== row.materialId) {
        this.formData.engineeringRequirementCode = ''
        this.formData.engineeringRequirementId = ''
        this.formData.engineeringProcessName = ''
        this.formData.engineeringProcessId = ''
        this.isShow = false
      }
      this.formData.materialCode = row.materialCode
      this.formData.materialName = row.materialName
      this.formData.materialModel = row.materialModel
      this.formData.materialModel = row.materialModel
      this.formData.materialUnitName = row.materialUnitName
      this.formData.productTaskCode = row.purchaseInspectCode
      this.formData.materialId = row.materialId
      this.tableData.filter(item => {
        const text = row.materialName
        item.materialName = text + '-' + item.id
      })
      if (this.tableData2 && this.tableData2.length > 0) {
        this.tableData2.filter(item => {
          item.materialName = row.materialName
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.formItem_header1 {
  height: 30px;
  width: 120px;
  line-height: 30px;
  font-size: 16px;
  font-weight: 400;
  color: #666;
  margin-left: 25px;
}
.expand_container{
  display: flex;
  .el-form-item{
    margin-bottom: 10px;
  }
}
.expand_radio{
  padding-top: 20px;
  /deep/ .el-form-item__content{
    text-align: left;
    display: flex;
  }
}
.cell{
  padding-left: 0px !important;
}
/deep/.el-radio {
  margin-right: 15px !important;
}
</style>
